package com.qf.web.controller;/**
 * @author Administrator
 * @version v1.0
 * @date 2022/4/18
 */

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * projectName:Project_2202
 * author:
 * time:2022/4/18 15:14
 * description:
 */

public class BaseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String param = request.getParameter("method");
        try {
            Method method = this.getClass().getMethod(param,HttpServletRequest.class,HttpServletResponse.class);
            String result=(String)method.invoke(this,request,response);
            if(result!=null){
                if(result.startsWith("redirect")){
                    String[] arr = result.split("/");
                    if(arr!=null&&arr.length>1){
                        response.sendRedirect(arr[1]);
                    }
                }else{
                    String[] arr = result.split(":");
                    if(arr!=null&&arr.length>1){
                        request.getRequestDispatcher(arr[1]).forward(request,response);
                    }
                }
            }
        } catch (Exception e) {
            request.getRequestDispatcher("/error/404.html").forward(request, response);
            return;
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }


}